package visualOne;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

public class UserNode implements Comparable<UserNode>{
	int id;
	double x;
	double y;
	Color c;
	int rad;
	private int connectivity;
	
	public UserNode(){
		this(0, 0, 0, Color.BLACK, 0);
	}
	
	public UserNode(int i, int xC, int yC, Color col, int s){
		id = i;
		x = xC;
		y = yC;
		c = col;
		rad = s;
	}
	
	public UserNode(int i, int xC, int yC, int s){
		this(i, xC, yC, Color.BLACK, s);
		int r = (int) (Math.random()*100+1);
		int g = (int) (Math.random()*100+1);
		int b = (int) (Math.random()*100+1);
		Color ran = new Color(155+r, 155+g, 155+b);
		c=ran;
	}
	
	public void draw(Graphics g){
		g.setColor(c);
		g.fillOval((int)x-rad, (int)y-rad, rad*2, rad*2);
		//System.out.println("rad: "+rad);
		Font font = new Font("Dialog", Font.PLAIN, (int)(12*rad/17.0));
		g.setFont(font);
		g.setColor(Color.BLACK);
		g.drawString(""+id, (int)x-rad+rad/6, (int)y+rad/6);
	}

	@Override
	public int compareTo(UserNode other) {
		return this.id-other.id;
	}
	
	public boolean contains(int xT, int yT){
		return(Math.sqrt((x-xT)*(x-xT)+(y-yT)*(y-yT))<rad);
	}

	public int getConnectivity() {
		return connectivity;
	}

	public void setConnectivity(int connectivity) {
		this.connectivity = connectivity;
	}
	
	public boolean seperate(UserNode other){
		double xT = other.x;
		double yT = other.y;
		double distance = Math.sqrt((x-xT)*(x-xT)+(y-yT)*(y-yT));
		if(Double.isNaN(xT))
			return true;
		System.out.println("dist+" +distance+" raddiff "+(this.rad+other.rad));
		if(distance>this.rad+other.rad){
			return false;
		}
		else{
			double xDiff = other.x-this.x;
			double yDiff = other.y-this.y;
			double angle = Math.atan2(yDiff, xDiff);
			double changeMag = (this.rad+other.rad)-distance+10;
			double xChange = (changeMag*Math.cos(angle))*xDiff/Math.abs(xDiff);
			double yChange = (changeMag*Math.sin(angle))*yDiff/Math.abs(yDiff);
			System.out.println(yChange);
			
			this.x-=xChange;
			this.y-=yChange;
			other.x+=xChange;
			other.y+=yChange;
			
			
			return true;
		}
	}
}
